package com.huanxing.cloud.mall.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.nacos.api.exception.NacosException;
import com.huanxing.cloud.common.core.desensitization.KeyDesensitization;
import com.huanxing.cloud.common.core.entity.Result;
import com.huanxing.cloud.common.nacos.util.NacosUtils;
import com.huanxing.cloud.mall.common.properties.MallConfigProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 商城配置
 *
 * @author lijx
 * @date 2022/6/13
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/config")
@Api(value = "config", tags = "商城配置")
public class MallConfigController {

	private final NacosUtils nacosUtils;

	private final KeyDesensitization keyDesensitization = new KeyDesensitization();

	@Value("${spring.profiles.active}")
	private String env;

	@ApiOperation(value = "商城配置查询")
	@SaCheckPermission("mall:mallconfig:get")
	@GetMapping
	public Result getConfig() {
		String configName = String.format("application-%s.yml", env);
		Map<String, Map<String, Map>> confMap = nacosUtils.getConfig(configName, "DEFAULT_GROUP");
		Object object = confMap.get("hx").get("mall");
		JSONObject jSONObject = JSONUtil.parseObj(object);
		MallConfigProperties mallConfigProperties = jSONObject.toBean(MallConfigProperties.class);
		if (StrUtil.isNotEmpty(mallConfigProperties.getLogisticsKey())) {
			mallConfigProperties.setLogisticsKey(keyDesensitization.serialize(mallConfigProperties.getLogisticsKey()));
		}
		return Result.success(mallConfigProperties);
	}

	@ApiOperation(value = "商城配置修改")
	@SaCheckPermission("mall:mallconfig:get")
	@PutMapping
	public Result edit(@RequestBody MallConfigProperties mallConfigProperties) throws NacosException {
		String configName = String.format("application-%s.yml", env);
		Map<String, Map<String, Map>> confMap = nacosUtils.getConfig(configName, "DEFAULT_GROUP");
		Object object = confMap.get("hx").get("mall");
		JSONObject jSONObject = JSONUtil.parseObj(object);
		if (!keyDesensitization.serialize(jSONObject.get("logisticsKey").toString())
			.equals(mallConfigProperties.getLogisticsKey())) {
			jSONObject.set("logisticsKey", mallConfigProperties.getLogisticsKey());
		}
		jSONObject.set("notifyDomain", mallConfigProperties.getNotifyDomain());
		jSONObject.set("orderTimeOut", mallConfigProperties.getOrderTimeOut());
		jSONObject.set("defaultAppraiseTime", mallConfigProperties.getDefaultAppraiseTime());
		jSONObject.set("defaultReceiverTime", mallConfigProperties.getDefaultReceiverTime());

		confMap.get("hx").put("mall", jSONObject);
		return Result.success(nacosUtils.publishConfig(configName, "DEFAULT_GROUP", confMap));
	}

}
